package top.itaso.app.ui.treelevel.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import top.itaso.app.R;
import top.itaso.lib.widget.vo.Node;
import top.itaso.lib.alog.ALog;

/**
 * @Description: 二级列表 ExpandableListView 适配器
 * @Author itsdf07
 * @E-Mail 923255742@qq.com
 * @Github https://github.com/itsdf07
 * @Date 2021/11/28
 */
public class ExpandableListViewAdapter extends BaseExpandableListAdapter {

    /**
     * 节点数据
     */
    private List<Node> mNodeHouses;
    /**
     * 复选框 Checkbox 监听事件
     */
    private CheckBoxListener mCheckBoxListener;

    /**
     * 用于刷新更新后的数据
     */
    public void refreshData(List<Node> dataBeans) {
        this.mNodeHouses = dataBeans;
        notifyDataSetChanged();
    }

    /**
     * @return 返回一级列表组的数量
     */
    @Override
    public int getGroupCount() {
        return mNodeHouses == null ? 0 : mNodeHouses.size();
    }

    /**
     * @param groupPosition 子元素组所在的父节点位置
     * @return 返回指定组中的子节点数量
     */
    @Override
    public int getChildrenCount(int groupPosition) {
        //此处 mNodeDatas 理论不需要判空了，因为getGroupCount已对 mNodeDatas 做判空处理
        List<Node> childrenNodeDatas = mNodeHouses.get(groupPosition).getChildren();
        return childrenNodeDatas == null ? 0 : childrenNodeDatas.size();
    }

    /**
     * 获取与给定组相关联的对象
     *
     * @param groupPosition 子元素组所在的位置
     * @return 返回指定组的子数据
     */
    @Override
    public Node getGroup(int groupPosition) {
        return mNodeHouses.get(groupPosition);
    }

    /**
     * 获取与给定组中的给定子元素关联的数据
     *
     * @param groupPosition 子元素组所在的位置
     * @param childPosition 子元素的位置
     * @return 返回子元素的对象
     */
    @Override
    public Node getChild(int groupPosition, int childPosition) {
        return (Node) mNodeHouses.get(groupPosition).getChildren().get(childPosition);
    }

    /**
     * 获取组在给定位置的ID（唯一的）
     *
     * @param groupPosition 子元素组所在的位置
     * @return 返回关联组ID
     */
    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }


    /**
     * 获取给定组中给定子元素的ID(唯一的)
     *
     * @param groupPosition 子元素组所在的位置
     * @param childPosition 子元素的位置
     * @return 返回子元素关联的ID
     */
    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    /**
     * @return 确定id 是否总是指向同一个对象
     */
    @Override
    public boolean hasStableIds() {
        return true;
    }

    /**
     * @return 返回指定组的对应的视图 （一级列表样式）
     */
    @Override
    public View getGroupView(final int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        ParentHolder parentHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_multi_level_expandable_parent_item, null);
            parentHolder = new ParentHolder();
            parentHolder.parentTitle = convertView.findViewById(R.id.tv_title);
            parentHolder.nextLevel = convertView.findViewById(R.id.next_level);
            convertView.setTag(parentHolder);
        } else {
            parentHolder = (ParentHolder) convertView.getTag();
        }
        Node nodeHouses = getGroup(groupPosition);

        parentHolder.parentTitle.setText(nodeHouses.getTitle() + "(共" + nodeHouses.getChildren().size() + "栋)");

        //共用一个右箭头，如果展开则顺时针旋转90°选择，否则不旋转
        if (isExpanded) {
            parentHolder.nextLevel.setRotation(90F);
        } else {
            parentHolder.nextLevel.setRotation(0F);
        }

        return convertView;
    }

    /**
     * @return 返回指定位置对应子视图的视图
     */
    @Override
    public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        final ChildrenHolder childrenHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_multi_level_expandable_child_item, null);
            childrenHolder = new ChildrenHolder();
            childrenHolder.childTitle = convertView.findViewById(R.id.tv_title);
            childrenHolder.checkBox = convertView.findViewById(R.id.checkbox);
            convertView.setTag(childrenHolder);
        } else {
            childrenHolder = (ChildrenHolder) convertView.getTag();
        }
        Node nodeBuild = getChild(groupPosition, childPosition);
        ArrayList<Node> nodeBuildRooms = (ArrayList<Node>) nodeBuild.getChildren();
        ALog.d("%s-共%s室", nodeBuild.getTitle(), nodeBuildRooms.size());
        childrenHolder.checkBox.setChecked(nodeBuild.isChecked());
        childrenHolder.childTitle.setText(nodeBuild.getTitle() + "(共" + nodeBuildRooms.size() + "室)");
        childrenHolder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                nodeBuild.setChecked(isChecked);
                ALog.d("groupPosition:%s,childPosition:%s,isChecked:%s", groupPosition, childPosition, isChecked);
                if (mCheckBoxListener != null) {
                    mCheckBoxListener.checkStateListener(groupPosition, childPosition, isChecked);
                }
            }
        });

//        childrenHolder.checkBox.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                boolean isChecked = !node.isChecked();
//                node.setChecked(!isChecked);
//                ALog.d("groupPosition:%s,childPosition:%s,isChecked:%s", groupPosition, childPosition, isChecked);
//                if (mCheckBoxListener != null) {
//                    mCheckBoxListener.checkStateListener(groupPosition, childPosition, isChecked);
//                }
//            }
//        });
        return convertView;
    }

    /**
     * 指定位置的子元素是否可选
     *
     * @param groupPosition 子元素组所在的位置
     * @param childPosition 子元素的位置
     * @return 返回是否可选
     */

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }


    class ParentHolder {
        TextView parentTitle;
        ImageView nextLevel;
    }


    class ChildrenHolder {
        TextView childTitle;
        CheckBox checkBox;
    }


    /**
     * 用于提供对外复选框修改通知接口
     */
    public interface CheckBoxListener {
        void checkStateListener(int groupPosition, int childPosition, boolean isChecked);
    }

    public void setCheckBoxListener(CheckBoxListener checkBoxListener) {
        this.mCheckBoxListener = checkBoxListener;
    }


}
